package com.yjr.gulimall.product.app;

import java.util.*;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjr.gulimall.product.entity.CategoryEntity;
import com.yjr.gulimall.product.service.CategoryService;
import com.yjr.common.utils.R;



/**
 * 商品三级分类
 *
 * @author yjr
 * @email 2608509744@qq.com
 * @date 2021-08-29 13:15:49
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有的父分类及其子分类，以树形结构组装
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R list(){

        List<CategoryEntity> categoryEntityList=categoryService.listWithTree();
        if(!categoryEntityList.isEmpty()){
            categoryEntityList.stream()
                    .filter(categoryEntity -> categoryEntity.getParentCid()==0)
                    .map(categoryEntity -> {categoryEntity.setChildren(getChildrens(categoryEntity,categoryEntityList));
                    return categoryEntity;
                    }

                    ).sorted((menu1,menu2)-> menu1.getSort()==null?0:menu1.getSort()-(menu2.getSort()==null?0:menu2.getSort()))
                    .collect(Collectors.toList());
        }
        return  R.ok().put("data",categoryEntityList);
    }

    private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> categoryEntityList) {
        List<CategoryEntity> childrens = categoryEntityList.stream().filter(category -> category.getParentCid() == root.getCatId()).map(
                        categoryEntity -> {
                            categoryEntity.setChildren(getChildrens(categoryEntity, categoryEntityList));
                            return categoryEntity;
                        }

                ).sorted((menu1, menu2) -> menu1.getSort() == null ? 0 : menu1.getSort() - (menu2.getSort() == null ? 0 : menu2.getSort()))
                .collect(Collectors.toList());
        return  childrens;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update/sort")
    //@RequiresPermissions("product:category:update")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));

        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		//categoryService.updateById(category);
        categoryService.updateCascade(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){

		//categoryService.removeByIds(Arrays.asList(catIds));

        categoryService.removeMeunByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
